﻿using AutoMapper;
using MediatR;
using Provenance.Domain.Product;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.Product;
using Provenance.Write.Api.Application.Command.Product;

namespace Provenance.Write.Api.Application.CommandHandler.Product
{
    public class InsertPictureInfoCommandHandler : IRequestHandler<InsertPictureInfoCommand, ApiResult>
    {
        private readonly IPictureInfoRepository _pictureInfoRepository;
        private readonly ILogger<InsertPictureInfoCommandHandler> _logger;
        private readonly IMapper _mapper;
        public InsertPictureInfoCommandHandler(IPictureInfoRepository pictureInfoRepository, ILogger<InsertPictureInfoCommandHandler> logger, IMapper mapper)
        {
            _pictureInfoRepository = pictureInfoRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(InsertPictureInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj= _mapper.Map<PictureInfo>(request);
                await _pictureInfoRepository.InsertEntity(obj);
                return ApiResult.Success();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error inserting picture info"+ex.Message);
                return ApiResult.Failure("Error inserting picture info"+ex.Message);
            }
        }
    }
}
